package com.situ.mall.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.pojo.entity.product.Category;
import com.situ.mall.product.mapper.CategoryMapper;
import com.situ.mall.product.pojo.vo.CategoryVO;
import com.situ.mall.product.service.ICategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author Z
 * @since 2024-10-09
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<CategoryVO> selectCategoryTree() {
        //1.查找所有分类，返回list集合
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_num");
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);

        List<CategoryVO> categoryVOList = new ArrayList<>();
        //箭头函数   Lambda表达式
        categoryList.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOList.add(categoryVO);
        });

        List<CategoryVO> categoryVOTree =  buildTree(categoryVOList);
        return categoryVOTree;
    }

    private List<CategoryVO> buildTree(List<CategoryVO> categoryVOList) {
        List<CategoryVO> treeList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(categoryVOList)) {
            for (CategoryVO categoryVO : categoryVOList) {
                //递归的入口是parentId=0
                if (categoryVO.getParentId() == 0) {
                    categoryVO.setChildren(buildChildTree(categoryVOList, categoryVO));
                    treeList.add(categoryVO);
                }
            }
        }
        return treeList;
    }

    private List<CategoryVO> buildChildTree(List<CategoryVO> categoryVOList , CategoryVO categoryVO) {
        List<CategoryVO> childTree = new ArrayList<>();
        if (!CollectionUtils.isEmpty(categoryVOList)) {
            for (CategoryVO categoryVO1 : categoryVOList) {
                if (categoryVO.getParentId().longValue() == categoryVO.getId()) {
                    categoryVO1.setChildren(buildChildTree(categoryVOList, categoryVO1));
                    childTree.add(categoryVO1);
                }
            }
        }
        return childTree;
    }

    @Cacheable(value = "category", key = "{#root.methodName, #id}")
    @Override
    public List<CategoryVO> selectByParentId(Long id) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_num");
        queryWrapper.eq("parent_id", id);
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        //2 把Category的值赋给CategoryVO
        List<CategoryVO> categoryVOS = new ArrayList<>();
        for (Category category : categories) {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            //判断其是否有孩子节点
            //当前categoryVO是存的父节点为id的值
            //现在查询所有的数据，有哪个数据的parent_id字段的值为当前数据的getId
            QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
            categoryQueryWrapper.eq("parent_id",categoryVO.getId());
            Long count = categoryMapper.selectCount(categoryQueryWrapper);
            if (count>0){
                categoryVO.setHasChildren(true);
            }else {
                categoryVO.setHasChildren(false);
            }
            categoryVOS.add(categoryVO);
        }
        return categoryVOS;
    }

    @Override
    public List<Category> selectAllCategory1() {
        //联合查询，查出category表中，跟product表的category1相等的数据
        List<Category> categoryList = categoryMapper.selectAllCategory1();
        return categoryList;
    }

    @CacheEvict(value = "category", allEntries = true)
    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    @CacheEvict(value = "category", allEntries = true)
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }
}
